package view.panel;

import java.awt.FlowLayout;
import java.awt.event.MouseEvent;
import java.util.Date;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import manager.CustomerManager;
import vo.Customer;
import adapter.MouseAdapter;
import exception.NotFoundException;

public class CustomerPanel extends JPanel {

	JTextField jfieldCustomerInfo, jfieldYear, jfieldMonth, jfieldDay, 
	jfieldCustomerId, jfieldCustomerName, jfieldPhone, jfieldBirth;
	JButton jbtnSearchCustomer, jbtnNewCustomer, jbtnSaveCustomer, jbtnDelCustomer;
	JLabel jlabelRegDate, jlabelYear, jlabelMonth, jlabelDay, jlabelCustomId, jlabelCustomName,
	jlabelPhone, jlabelBirth;

	public CustomerPanel() {

		setLayout(new FlowLayout(FlowLayout.LEFT, 10, 30));
		setSize(300,200);
		setVisible(true);

		jfieldBirth = new JTextField("생년월일",12);
		jfieldCustomerId = new JTextField("고객번호",8);
		jfieldCustomerName = new JTextField("고객명",4);
		jfieldDay = new JTextField("일",4);
		jfieldMonth = new JTextField("월",4);
		jfieldYear = new JTextField("년",4);
		jfieldPhone = new JTextField("전화번호",16);
		jfieldCustomerInfo = new JTextField("고객정보",8);

		jbtnNewCustomer = new JButton("새 고객");
		jbtnSaveCustomer = new JButton("저장");
		jbtnSearchCustomer = new JButton("고객정보검색");
		jbtnDelCustomer = new JButton("삭제");

		jlabelBirth = new JLabel("생일");
		jlabelCustomId = new JLabel("고객번호");
		jlabelCustomName = new JLabel("고객명");
		jlabelYear = new JLabel("년");
		jlabelMonth = new JLabel("월");
		jlabelDay = new JLabel("일");
		jlabelPhone = new JLabel("전화번호");
		jlabelRegDate = new JLabel("가입일");

		add(jfieldCustomerInfo);

		add(jlabelRegDate);		
		add(jfieldYear);		
		add(jlabelYear);
		add(jfieldMonth);
		add(jlabelMonth);
		add(jfieldDay);
		add(jlabelDay);
		add(jlabelCustomId);
		add(jfieldCustomerId);
		add(jlabelCustomName);
		add(jfieldCustomerName);
		add(jlabelPhone);
		add(jfieldPhone);
		add(jlabelBirth);
		add(jfieldBirth);


		add(jbtnSearchCustomer);
		add(jbtnNewCustomer);
		add(jbtnDelCustomer);
		add(jbtnSaveCustomer);
		
		jbtnNewCustomer.addMouseListener(new MouseAdapter() {
			
			@Override
			public void mouseClicked(MouseEvent e) {
				
				Customer customer = new Customer();
				
				customer.setName(jfieldCustomerName.getText());
				customer.setId(Integer.parseInt(jfieldCustomerId.getText()));
								
				CustomerManager.getInstance().registCustomer(customer);
			}
			
		});
				

		jbtnSearchCustomer.addMouseListener(new MouseAdapter() { 
			@Override
			public void mouseClicked(MouseEvent e) {

				int findCustomerId = Integer.parseInt(jfieldCustomerInfo.getText());		
				Customer customer;

				Date regDate;
				String customerName;
				int customerId;
				String phone;
				Date birthDate;


				try {
					customer = CustomerManager.getInstance().findCustomer(findCustomerId);
					
					regDate = customer.getDate();
					customerName = customer.getName();
					customerId = customer.getId();
					phone = customer.getTelPhone();
					birthDate = customer.getBirthDate();		
					
				} catch (NotFoundException e1) {
					System.out.println(e1.getMessage());
				}
			
				//jfieldCustomerName.setText(t);


			}
		});
	}

}
